import type { App } from "vue";
import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";

export const RootRoute: RouteRecordRaw = {
    path: '',
    name: 'Root',
    component:() => import('../views/home.vue'),
    meta: { title: 'Home' }
};
export const DemoRoute: RouteRecordRaw = {
    path: '/demo',
    name: 'Demo',
    component:() => import('../views/demo.vue'),
    meta: { title: 'Demo' }
};
export const PrimitiveRoute: RouteRecordRaw = {
    path: '/primitive',
    name: 'Primitive',
    component:() => import('../views/primitive.vue'),
    meta: { title: '图元' }
};
export const TestRoute: RouteRecordRaw = {
    path: '/test',
    name: 'Test',
    component:() => import('../views/test.vue'),
    meta: { title: '测试' }
};

//普通路由 无需验证权限
export const constantRouter: RouteRecordRaw[] = [
    RootRoute,
    DemoRoute,
    PrimitiveRoute,
    TestRoute
];

const router = createRouter({
    history: createWebHashHistory(),
    routes: constantRouter,
    strict: true,
    scrollBehavior: () => ({ left: 0, top: 0 }),
});

export function setupRouter(app: App) {
    app.use(router);
    router.beforeEach((to,from,next)=>{
        if (to.meta.title) {
            document.title = to.meta.title as string;
        }
        next();
    })
}

export default router;